<template>
  <el-container class="home-container">
    <!-- header区域 -->
    <el-header>
      <div>Home Header</div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧悬浮框(双层v-for循环渲染左侧栏) -->
      <el-aside :width="btnCollapseBool ? '64px' :'200px'">
        <div @click="btnCollapse" class="btn-collapse" :title="btnCollapseBool ? '展开' :'收起'">
          <i :class="btnCollapseBool ? 'el-icon-d-arrow-right' :'el-icon-d-arrow-left'"></i>
        </div>
        <el-menu background-color="rgb(236, 245, 255)" text-color="#303133" active-text-color="#409EFF" :default-active='activePath' unique-opened :collapse="btnCollapseBool" :collapse-transition='false' router>
          <!-- 一级目录 -->
          <!-- index只接收字符串，将item.id数组加上空字符串变成字符串item.id + '' -->
          <el-submenu :index="item.id + ''" v-for='item in menulist' :key="item.id">
            <!-- 一级目录模板 -->
            <template #title>
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级目录 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveActivePath('/' + subItem.path)">
              <!-- 二级目录模板 -->
              <template #title>
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区域 -->
      <el-main>
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
    //   菜单存储数组
      menulist: [],
      //   icon类名存储
      iconObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-connection',
        101: 'el-icon-sell',
        102: 'el-icon-s-order',
        145: 'el-icon-data-line'
      },
      //   侧边栏是否折叠的布尔值
      btnCollapseBool: false,
      activePath: ''
    }
  },
  // 生命周期函数，组件创建的时候执行
  created() {
    // 执行该函数
    this.getMenuList()
    // 将sessionStorage的activePath赋值到activePath
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出逻辑：清除token，重定向至login
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    // 异步获取数据
    async getMenuList() {
      // 解构一个data获取res里面的数据
      const { data: res } = await this.$axios.get('menus')
      // 如果status返回值不为200，则将数据赋值给menulist
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res.data)
    },
    // 点击按钮，切换菜单的折叠和打开
    btnCollapse() {
      this.btnCollapseBool = !this.btnCollapseBool
    },
    // 将当前打开的组件path值添加到sessionStorage，并且赋值到activePath，展示高亮
    saveActivePath(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header,
.el-footer {
  background-color: #409EFF;
  color: #333;
  line-height: 60px;
}

.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    font-size: 24px;
    font-weight: bold;
    color: #ffffff;
  }
}

.el-aside {
  background-color: rgb(236, 245, 255);
  color: #333;
  // text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  // text-align: center;
//   line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.btn-collapse {
  width: 100%;
  height: 24px;
  line-height: 24px;
  text-align: right;
  padding-right: 20px;
  box-sizing: border-box;
  cursor: pointer;
  background-color: #74b8fc;
}
</style>
